# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from wxapp.items import WxappItem


class WxappSpiderSpider(CrawlSpider):
    name = 'wxapp_spider'
    allowed_domains = ['wxapp-union.com']
    start_urls = ['http://wxapp-union.com/portal.php?mod=list&catid=1&page=1']
    # 提取所有的页码
    # allow=(allow=r'http://www.wxapp-union.com/portal.php?mod=list&catid=1&page=\d')
    rules = (
        Rule(LinkExtractor(allow=r'.+mod=list&catid=1&page=\d'), follow=True),
        Rule(LinkExtractor(allow=r'.+article-.+\.html'),callback='parse_detail',follow=False)
    )

    def parse_detail(self, response):
        title = response.xpath('//h1[@class="ph"]/text()').get()
        # 测试title
        author_p = response.xpath('//p[@class="authors"]')
        author = author_p.xpath('.//a/text()').get()
        pub_time = author_p.xpath('.//span/text()').get()
        #测试 author、pub_time
        # print('author:%s' % author)
        # print('pub_time:%s'% pub_time)
        # print('='*30)
        article_content = response.xpath('//td[@id="article_content"]//text()').getall()
        # print(article_content)
        content = "".join(article_content).strip()
        # print(content)
        # item = {}
        # #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
        # #item['name'] = response.xpath('//div[@id="name"]').get()
        # #item['description'] = response.xpath('//div[@id="description"]').get()
        # return item
        item = WxappItem(title=title,author=author,pub_time=pub_time,content=content)
        yield item
